package sf.dsl.example;

import sf.database.dialect.DBDialect;
import sf.database.jdbc.sql.SQLContext;
import sf.database.jdbc.sql.SQLParameter;

import java.util.List;

public class UnionBuilder {

    private String key;
    private Example query;

    public static UnionBuilder union(Example query) {
        UnionBuilder unionBuilder = new UnionBuilder();
        unionBuilder.key = " UNION ";
        unionBuilder.query = query;
        return unionBuilder;
    }

    public static UnionBuilder unionAll(Example query) {
        UnionBuilder unionBuilder = new UnionBuilder();
        unionBuilder.key = " UNION ALL ";
        unionBuilder.query = query;
        return unionBuilder;
    }


    private UnionBuilder() {
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Example getQuery() {
        return query;
    }

    public void setQuery(Example query) {
        this.query = query;
    }

    public void buildSql(StringBuilder sqlBuilder, List<SQLParameter> list, DBDialect dialect) {
        query.dialect = dialect;
        SQLContext sqlContext = ExampleSQL.getSelectSQLContext(query, null, SelectOpt.all, true);
        list.addAll(sqlContext.getParas());
        sqlBuilder.append(key).append('(').append(sqlContext.getSql()).append(')');
    }
}
